#include <REGX52.H>
#include "timer.c"
/**
 * 灯一开始是常亮状态
 * 按一下变为闪烁。
 * 按一下熄灭。
 * 按一下常亮。
 * 三个状态切换
 *
 */

void delay_ms(unsigned int ms)
{
    unsigned int i, j;
    for (i = 0; i < ms; i++)
    {
        for (j = 0; j < 123; j++)
            ;
    }
}

sbit BTN = P2 ^ 6;
sbit LED = P2 ^ 0;

volatile int ms_count = 0;
// 0 常亮， 1 熄灭, 2 闪烁
unsigned char status = 0;

void timer0_ISR() interrupt 1
{
    TH0 = 0xFC;
    TL0 = 0x66;
    if (status == 2)
    {
        ms_count++;
    }
}

void main()
{
    timer0_init();
    timer0_isr_on();
    LED = 1;
    while (1)
    {
        // 按下
        if (BTN == 0)
        {
            while (BTN == 0)
            {
            }

            status = (status + 1) % 3;

            if (status == 0)
            {
                LED = 1;
            }
            else if (status == 1)
            {
                LED = 0;
            }
        }

        if (status == 2)
        {
            if (ms_count > 1300)
            {
                ms_count = 0;
                LED = ~LED;
            }
        }
    }
}